package com.gzbugu.bugusys.action;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.dao.IHQLQuery;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.StaffCertificate;
import com.gzbugu.bugusys.vo.StaffEducation;
import com.gzbugu.bugusys.vo.StaffExperience;
import com.gzbugu.bugusys.vo.StaffFamily;
import com.gzbugu.bugusys.util.HibernateUtils;
import com.opensymphony.xwork2.ActionSupport;

@Transactional
public class UpdateStaffAction extends ActionSupport {
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;
	@Resource(name = "hqlQuery")
	IHQLQuery hqlQuery;

	private Staff staff;
	private String staffIded;
	private String deleteId;
	private List<String> staffCertificateName;
	private List<Date> staffCertificateIssueDate;
	private List<Date> staffCertificateNotbefore;
	private List<Date> staffCertificateNotafter;
	private List<Date> staffEducationStart;
	private List<Date> staffEducationEnd;
	private List<String> staffEducationSchool;
	private List<String> staffEducationReference;
	private List<Date> staffExperienceStart;
	private List<Date> staffExperienceEnd;
	private List<String> staffExperienceUnit;
	private List<String> staffExperiencePost;
	private List<String> staffFamilyName;
	private List<String> staffFamilyRelationship;
	private List<String> staffFamilyUnit;
	private List<String> staffFamilyPost;
	private List<Integer> staffFamilyId;
	private List<Integer> staffEducationId;
	private List<Integer> staffExperienceId;
	private List<Integer> staffCertificateId;
	private List<String> errorList;
	private Short staffSexId;
	private Integer staffSex;

	public String execute() {
		errorList = new ArrayList<String>();
		Session session = null;
		Transaction tx = null;
		System.out.println(invalidate());
		if (invalidate()) {
			try {
				session = HibernateUtils.getSession();
				tx = session.beginTransaction();
				staff.setSex(staffSex);
				List list = session.createQuery(
						"from Staff staff where staff.staffId="
								+ staff.getStaffId()).list();
				Staff staffPer = (Staff) list.get(0);
				this.saveDemo(session, staffPer, true);
				tx.commit();
			} catch (HibernateException e) {
				e.printStackTrace();
				tx.rollback();
			} finally {
				HibernateUtils.closeSession(session);
			}
		}
		return SUCCESS;
	}

	public String deleteStaff() {
		if (deleteId != null && !("".equals(deleteId))) {
			Staff staffed = (Staff) baseDAO.findById(deleteId,
					com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
			if (staffed != null) {
				baseDAO.delete(staffed);
			}
		}
		return SUCCESS;
	}

	public void saveDemo(Session session, Staff staffPer, boolean flag) {
		staffPer.setStaffId(staff.getStaffId());
		staffPer.setIdnumber(staff.getIdnumber());
		staffPer.setAddress(staff.getAddress());
		staffPer.setBirthday(staff.getBirthday());
		staffPer.setBirthplace(staff.getBirthplace());
		staffPer.setDepartment(staff.getDepartment());
		staffPer.setEducation(staff.getEducation());
		staffPer.setFormername(staff.getFormername());
		staffPer.setGraduateSchool(staff.getGraduateSchool());
		staffPer.setHiredate(staff.getHiredate());
		staffPer.setName(staff.getName());
		staffPer.setNation(staff.getNation());
		staffPer.setNativePlace(staff.getNativePlace());
		staffPer.setPoliticsStatus(staff.getPoliticsStatus());
		staffPer.setPost(staff.getPost());
		staffPer.setSex(staffSex);
		staffPer.setTel(staff.getTel());
		staffPer.setZip(staff.getZip());
		if (flag) {
			session.update(staffPer);
		} else {
			session.save(staffPer);
		}

		for (int j = 0; j <= 2; j++) {
			List list1 = session.createQuery(
					"from StaffFamily family where family.staffFamilyId="
							+ staffFamilyId.get(j)).list();
			StaffFamily staffFamily = (StaffFamily) list1.get(0);
			staffFamily.setStaff(staffPer);
			staffFamily.setName(staffFamilyName.get(j));
			staffFamily.setPost(staffFamilyPost.get(j));
			staffFamily.setUnit(staffFamilyUnit.get(j));
			staffFamily.setRelationship(staffFamilyRelationship.get(j));
			session.update(staffFamily);
		}
		for (int x = 0; x <= 2; x++) {
			List list2 = session.createQuery(
					"from StaffEducation edu where edu.staffEducationId="
							+ staffEducationId.get(x)).list();
			StaffEducation staffEducation = (StaffEducation) list2.get(0);
			staffEducation.setStaff(staffPer);
			staffEducation.setStart(staffEducationStart.get(x));
			staffEducation.setEnd(staffEducationEnd.get(x));
			staffEducation.setSchool(staffEducationSchool.get(x));
			staffEducation.setReference(staffEducationReference.get(x));
			session.update(staffEducation);
		}
		for (int y = 0; y <= 2; y++) {
			List list3 = session.createQuery(
					"from StaffExperience ex where ex.staffExperienceId ="
							+ staffExperienceId.get(y)).list();
			StaffExperience staffExperience = (StaffExperience) list3.get(0);
			staffExperience.setStaff(staffPer);
			staffExperience.setStart(staffExperienceStart.get(y));
			staffExperience.setEnd(staffExperienceEnd.get(y));
			staffExperience.setPost(staffExperiencePost.get(y));
			staffExperience.setUnit(staffExperienceUnit.get(y));
			session.update(staffExperience);
		}
		for (int z = 0; z <= 2; z++) {
			List list4 = session.createQuery(
					"from StaffCertificate cer where cer.staffCertificateId ="
							+ staffCertificateId.get(z)).list();
			StaffCertificate staffCertificate = (StaffCertificate) list4.get(0);
			staffCertificate.setStaff(staffPer);
			staffCertificate.setName(staffCertificateName.get(z));
			staffCertificate.setIssueDate(staffCertificateIssueDate.get(z));
			staffCertificate.setNotafter(staffCertificateNotafter.get(z));
			staffCertificate.setNotbefore(staffCertificateNotbefore.get(z));
			session.update(staffCertificate);
		}
	}

	public boolean invalidate() {
		boolean flag = true;
		// 员工姓名不能为空
		if (this.staff.getName() == null || "".equals(this.staff.getName())) {
			this.errorList.add("员工姓名不能为空！");
			flag = false;
		}
		// 员工编号不能为空
		if (this.staff.getStaffId() == null
				|| "".equals(this.staff.getStaffId())) {
			this.errorList.add("员工编号不能为空！");
			flag = false;
		}
		// 政治面貌需选择

		// 员工性别不能为空
		if (this.staffSex == null) {
			this.errorList.add("员工性别不能为空！");
			flag = false;
		} else {
			if (!(staffSex == 2) && !(staffSex == 3)) {
				this.errorList.add("请正确改写性别！");
				flag = false;
			}
		}

		for (int i = 0; i <= 2; i++) {
			if (staffEducationStart != null && staffEducationEnd != null
					&& staffEducationSchool != null
					&& staffEducationReference != null) {
				Date start = (Date) staffEducationStart.get(i);
				Date end = (Date) staffEducationEnd.get(i);
				String school = (String) staffEducationSchool.get(i);
				String reference = (String) staffEducationReference.get(i);
				if (start != null && !("".equals(start))) {
					if (end == null || "".equals(end) || school == null
							|| "".equals(school) || reference == null
							|| "".equals(reference)) {
						this.errorList.add("员工学历资料不完整！");
						flag = false;
					}
					if (end.getTime() - start.getTime() <= 0) {
						this.errorList.add("您输入的教育经历起止时间不符合实际！");
						System.out.println("2");
						flag = false;
					}
				}
			}
			if (staffExperienceStart != null && staffExperienceEnd != null
					&& staffExperiencePost != null
					&& staffExperienceUnit != null) {
				Date exStart = (Date) staffExperienceStart.get(i);
				Date exEnd = (Date) staffExperienceEnd.get(i);
				String post = (String) staffExperiencePost.get(i);
				String unit = (String) staffExperienceUnit.get(i);

				if (exStart != null && !("".equals(exStart))) {
					if (exEnd == null || "".equals(exEnd) || unit == null
							|| "".equals(unit) || post == null
							|| "".equals(post)) {
						this.errorList.add("员工工作经历资料不完整！");
						System.out.println("3");
						flag = false;
					}
					if (exEnd.getTime() - exStart.getTime() <= 0) {
						this.errorList.add("您的工作经历起止时间不符合实际");
						System.out.println("4");
						flag = false;
					}

				}
			}
			if (staffFamilyName != null && staffFamilyPost != null
					&& staffFamilyUnit != null
					&& staffFamilyRelationship != null) {
				String fName = (String) staffFamilyName.get(i);
				String fPost = (String) staffFamilyPost.get(i);
				String fUnit = (String) staffFamilyUnit.get(i);
				String relationship = (String) staffFamilyRelationship.get(i);
				if (fName != null && !("".equals(fName))) {
					if (fPost == null || "".equals(fPost) || fUnit == null
							|| "".equals(fUnit) || relationship == null
							|| "".equals(relationship)) {
						this.errorList.add("员工家庭成员资料不完整！");
						System.out.println("5");
						flag = false;
					}
				}
			}
			if (staffCertificateIssueDate != null
					&& staffCertificateNotbefore != null
					&& staffCertificateNotafter != null
					&& staffCertificateName != null) {
				Date issueDate = (Date) staffCertificateIssueDate.get(i);
				Date notbefore = (Date) staffCertificateNotbefore.get(i);
				Date notafter = (Date) staffCertificateNotafter.get(i);
				String cerName = (String) staffCertificateName.get(i);

				if (issueDate != null && !("".equals(issueDate))) {
					if (notafter == null || "".equals(notafter)
							|| notbefore == null || "".equals(notbefore)
							|| cerName == null || "".equals(cerName)) {
						this.errorList.add("员工证书资料不完整！");
						System.out.println("6");
						flag = false;
					}

					if (notafter.getTime() - notbefore.getTime() <= 0) {
						this.errorList.add("您的证书有效起止时间不符合实际！");
						System.out.println("7");
						flag = false;
					}
					if (notbefore.getTime() - issueDate.getTime() <= 0) {
						this.errorList.add("您的证书获得时间与开始有效时间冲突！");
						System.out.println("8");
						flag = false;
					}
				}
			}
		}
		return flag;

	}

	public List<String> getErrorList() {
		return errorList;
	}

	public void setErrorList(List<String> errorList) {
		this.errorList = errorList;
	}

	public Short getStaffSexId() {
		return staffSexId;
	}

	public void setStaffSexId(Short staffSexId) {
		this.staffSexId = staffSexId;
	}

	public Integer getStaffSex() {
		return staffSex;
	}

	public void setStaffSex(Integer staffSex) {
		this.staffSex = staffSex;
	}

	public String getStaffIded() {
		return staffIded;
	}

	public void setStaffIded(String staffIded) {
		this.staffIded = staffIded;
	}

	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public List<String> getStaffCertificateName() {
		return staffCertificateName;
	}

	public void setStaffCertificateName(List<String> staffCertificateName) {
		this.staffCertificateName = staffCertificateName;
	}

	public List<Date> getStaffCertificateIssueDate() {
		return staffCertificateIssueDate;
	}

	public void setStaffCertificateIssueDate(
			List<Date> staffCertificateIssueDate) {
		this.staffCertificateIssueDate = staffCertificateIssueDate;
	}

	public List<Date> getStaffCertificateNotbefore() {
		return staffCertificateNotbefore;
	}

	public void setStaffCertificateNotbefore(
			List<Date> staffCertificateNotbefore) {
		this.staffCertificateNotbefore = staffCertificateNotbefore;
	}

	public List<Date> getStaffCertificateNotafter() {
		return staffCertificateNotafter;
	}

	public void setStaffCertificateNotafter(List<Date> staffCertificateNotafter) {
		this.staffCertificateNotafter = staffCertificateNotafter;
	}

	public List<Date> getStaffEducationStart() {
		return staffEducationStart;
	}

	public void setStaffEducationStart(List<Date> staffEducationStart) {
		this.staffEducationStart = staffEducationStart;
	}

	public List<Date> getStaffEducationEnd() {
		return staffEducationEnd;
	}

	public void setStaffEducationEnd(List<Date> staffEducationEnd) {
		this.staffEducationEnd = staffEducationEnd;
	}

	public List<String> getStaffEducationSchool() {
		return staffEducationSchool;
	}

	public void setStaffEducationSchool(List<String> staffEducationSchool) {
		this.staffEducationSchool = staffEducationSchool;
	}

	public List<String> getStaffEducationReference() {
		return staffEducationReference;
	}

	public void setStaffEducationReference(List<String> staffEducationReference) {
		this.staffEducationReference = staffEducationReference;
	}

	public List<Date> getStaffExperienceStart() {
		return staffExperienceStart;
	}

	public void setStaffExperienceStart(List<Date> staffExperienceStart) {
		this.staffExperienceStart = staffExperienceStart;
	}

	public List<Date> getStaffExperienceEnd() {
		return staffExperienceEnd;
	}

	public void setStaffExperienceEnd(List<Date> staffExperienceEnd) {
		this.staffExperienceEnd = staffExperienceEnd;
	}

	public List<String> getStaffExperienceUnit() {
		return staffExperienceUnit;
	}

	public void setStaffExperienceUnit(List<String> staffExperienceUnit) {
		this.staffExperienceUnit = staffExperienceUnit;
	}

	public List<String> getStaffExperiencePost() {
		return staffExperiencePost;
	}

	public void setStaffExperiencePost(List<String> staffExperiencePost) {
		this.staffExperiencePost = staffExperiencePost;
	}

	public List<String> getStaffFamilyName() {
		return staffFamilyName;
	}

	public void setStaffFamilyName(List<String> staffFamilyName) {
		this.staffFamilyName = staffFamilyName;
	}

	public List<String> getStaffFamilyRelationship() {
		return staffFamilyRelationship;
	}

	public void setStaffFamilyRelationship(List<String> staffFamilyRelationship) {
		this.staffFamilyRelationship = staffFamilyRelationship;
	}

	public List<String> getStaffFamilyUnit() {
		return staffFamilyUnit;
	}

	public void setStaffFamilyUnit(List<String> staffFamilyUnit) {
		this.staffFamilyUnit = staffFamilyUnit;
	}

	public List<String> getStaffFamilyPost() {
		return staffFamilyPost;
	}

	public void setStaffFamilyPost(List<String> staffFamilyPost) {
		this.staffFamilyPost = staffFamilyPost;
	}

	public List<Integer> getStaffFamilyId() {
		return staffFamilyId;
	}

	public void setStaffFamilyId(List<Integer> staffFamilyId) {
		this.staffFamilyId = staffFamilyId;
	}

	public List<Integer> getStaffEducationId() {
		return staffEducationId;
	}

	public void setStaffEducationId(List<Integer> staffEducationId) {
		this.staffEducationId = staffEducationId;
	}

	public List<Integer> getStaffExperienceId() {
		return staffExperienceId;
	}

	public void setStaffExperienceId(List<Integer> staffExperienceId) {
		this.staffExperienceId = staffExperienceId;
	}

	public List<Integer> getStaffCertificateId() {
		return staffCertificateId;
	}

	public void setStaffCertificateId(List<Integer> staffCertificateId) {
		this.staffCertificateId = staffCertificateId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

}
